<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2023/5/4
 * Time: 13:54
 * Desc: 更新产品波动
 */
declare (strict_types=1);

namespace app\command;

use app\extend\kline\OpenExchangeRate;
use app\extend\model\Product;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Productdata extends Command
{
    protected function configure()
    {
        $this->setName('productdata')->setDescription('更新产品的波动数据');
    }

    protected function execute(Input $input, Output $output)
    {
        $productArr = Product::where([
            ['is_delete', '=', 0]
        ])->field('id,product_name,product_name_en,point_low,point_top,type,rands,sale')->select()->toArray();

        if (empty($productArr)) {
            exit('暂无产品处理');
        }

        //是否有需要拉取openExchangeRates
        $isOpenExchangeRates = Product::where([
            ['is_delete', '=', 0],
            ['type','=','OpenExchangeRates']
        ])->value('id');

        $openExchangeRatesArr = [];
        if(!empty($isOpenExchangeRates)){
            $openExchangeRatesArr = OpenExchangeRate::getPrice();
        }

        //循环数据
        foreach ($productArr as $productVal) {
            $rand = randNum((float)$productVal['rands']) - (float)$productVal['rands'] / 2;

            if($productVal['type'] == 'Manual'){ //手动
                \app\extend\model\ProductData::where([
                    ['product_id', '=', $productVal['id']]
                ])->update([
                    'price' => fengkong($productVal['sale'] + $rand, $productVal),
                    'open' => $productVal['sale'],
                    'close' => $productVal['sale'] - $productVal['point_low'],
                    'high' => $productVal['sale'] + $productVal['point_top'],
                    'low' => $productVal['sale'] - $productVal['point_low'],
                    'name' => $productVal['product_name']
                ]);

                echo "手动：ID：{$productVal['id']}更新成功\n";
            } else {
                if($productVal['type'] == 'OpenExchangeRates') {
                    $sale = isset($openExchangeRatesArr[$productVal['product_name_en']]) ? $openExchangeRatesArr[$productVal['product_name_en']] : 0;

                    $saveData = [
                        'price' => fengkong($sale + $rand, $productVal),
                        'open' => $sale,
                        'close' => $sale - $productVal['point_low'],
                        'high' => $sale + $productVal['point_top'],
                        'low' => $sale - $productVal['point_low'],
                        'name' => $productVal['product_name']
                    ];
                } else {
                    $className = '\\app\\extend\\kline\\'.$productVal['type'];
                    $result = $className::getPrice($productVal['product_name_en']);

                    $sale = $result['price'];

                    $saveData = [
                        'price' => fengkong($sale + $rand, $productVal),
                        'open' => $result['open'],
                        'close' => $result['close'],
                        'high' => $result['high'],
                        'low' => $result['low'],
                        'name' => $productVal['product_name']
                    ];
                }

                \app\extend\model\ProductData::where([
                    ['product_id', '=', $productVal['id']]
                ])->update($saveData);

                echo $productVal['type']."：ID：{$productVal['id']}更新成功\n";
            }
        }
    }
}